# This controller edits the newspaper's contents
# 
# Workflow:
# 
# 1. The editor logs in. The masthead has a Preferences panel where they can change
#    account memberships and assorted other preferences.
# 2. If there aren't any volumes, they're asked to make one, and they do.
# 3. If there aren't any issues, they're asked to make one, and they do.
# 4. They are presented with the issue editing screen; from here they can 
#    make sections and articles, and attach them to each other.
#
#    By default, the Front Page and Trash sections are present and cannot be deleted.
#
# 5. From the issue editing screen, articles can be clicked on and edited in their
#    own screens. The suggestion is that articles are opened in their own windows or tabs.
#    (Make them open up in windows by default should be in the preferenes.)
#
class EditController < ApplicationController
  
  # TODO: switch the use of volume and issue out of being stored in a session
  # and into being stored in the params string
  
  issue_methods = [:list_issues, :show_issue, :new_issue, :create_issue]
  article_methods = [:list_articles, :show_articles, :new_articles, :create_articles]
  
  before_filter :login_required
  before_filter :volume_selected?, :only => issue_methods + article_methods
  before_filter :issue_selected?, :only => article_methods

  def index
    latest_issue
    render :action => 'latest_issue'
  end
  
  # CRUD for volumes
  def list_volumes
    @volumes = Volume.find_all
  end
  
  def show_volume
    @volume = Volume.find(params[:id])
  end
  
  def new_volume
    @volume = Volume.new
  end
  
  def create_volume
    @volume = Volume.new(params[:volume])
    if @volume.save
      flash[:notice] = "Volume #{@volume.name} was successfully created"
      if Preference.streamline_edit?
        # I'm unsure how I feel about these redirects.
        # Maybe having
        #   list_issues(@volume)
        #   render :action => 'list_issues'
        # would be better.
        
        list_issues(@volume)
        render :action => 'list_issues'
        
        #redirect_to :action => 'list_issues', :volume => @volume
      else
        redirect_to :action => 'list_volumes'
      end
    end
  end
  
  # CRUD for issues
  # issue methods all have session[:volume] == true
  def list_issues(volume = latest_volume)
    unless volume.nil?
      @issues = volume.issues
    else
      redirect_to :action => 'volumes'
    end
  end
  
  def show_issue
    
  end
  
  def new_issue
    
  end
  
  def create_issue
    
  end
  
  # TODO
  # CRUD for articles
  # article methods all have session[:issue] == true && session[:volume] == true
  def article
    @article = article
  end
  
  private
  
  # Returns the latest volume
  def latest_volume
    Volume.find(:first, :order => "updated_at DESC")
  end
  
  # Returns the latest issue
  def latest_issue
    Issue.find(:first, :order => "updated_at DESC")
  end
  
  #TODO
  # Checks to see if there's a selected volume
  def volume_selected?
  end
  
  #TODO
  # Checks to see if there's a selected issue
  def issue_selected?
  end
end